package com.xnyzc.lhy.common.component.interceptor;

import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.handlers.AbstractSqlParserHandler;
import com.xnyzc.lhy.common.entity.DataScope;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Sb;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.ListUtil;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 数据权限拦截器，若请求参数包含分割字段，则自动添加数据权限sql到执行语句。
 */
@Intercepts(value = {@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class DataScopeInterceptor extends AbstractSqlParserHandler implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
        this.sqlParser(metaObject);
        // 先判断是不是SELECT操作
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
            return invocation.proceed();
        }

        BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
        String originalSql = boundSql.getSql();
        Object parameterObject = boundSql.getParameterObject();

        //查找参数中包含DataScope类型的参数
        DataScope dataScope = findDataScopeObject(parameterObject);

        if (dataScope == null) {
            return invocation.proceed();
        }
        else {
            String scopeName = dataScope.getScopeName();

            if (DataScope.ADCODE.equals(scopeName)) {
                List<String> adcodes = dataScope.getAdcodes();
                if (CheckUtil.strIsNotEmpty(scopeName) && CheckUtil.collectionIsNotEmpty(adcodes)) {

                    Sb sb = Sb.create();
                    sb.add("select * from (").add(originalSql).add(") temp_data_scope where emp_data_scope.").add(scopeName).add(" in (");
                    sb.add(ListUtil.join(adcodes));
                    sb.add(")");

                    metaObject.setValue("delegate.boundSql.sql", sb.toString());
                }
            }


            return invocation.proceed();
        }
    }

    /**
     * 生成拦截对象的代理
     *
     * @param target 目标对象
     * @return 代理对象
     */
    @Override
    public Object plugin(Object target) {
        if (target instanceof StatementHandler) {
            return Plugin.wrap(target, this);
        }
        return target;
    }

    /**
     * mybatis配置的属性
     *
     * @param properties mybatis配置的属性
     */
    @Override
    public void setProperties(Properties properties) {

    }

    /**
     * 查找参数是否包括DataScope对象
     *
     * @param parameterObj 参数列表
     * @return DataScope
     */
    private DataScope findDataScopeObject(Object parameterObj) {
        if (parameterObj instanceof DataScope) {
            return (DataScope) parameterObj;
        }
        else if (parameterObj instanceof Qw) {
            return ((Qw) parameterObj).getDataScope();
        }
        else if (parameterObj instanceof Map) {
            for (Object val : ((Map<?, ?>) parameterObj).values()) {
                if (val instanceof DataScope) {
                    return (DataScope) val;
                }
                else if (val instanceof Qw) {
                    return ((Qw) val).getDataScope();
                }
            }
        }
        return null;
    }

}
